package com.ruoyi;

import com.ruoyi.jim.listener.ClientListener;
import com.ruoyi.jim.listener.GroupListener;
import com.ruoyi.jim.listener.ServerListener;
import com.ruoyi.jim.processor.JIMWsHandshakeProcessor;
import com.ruoyi.jim.processor.LoginServiceProcessor;
import org.jim.core.packets.Command;
import org.jim.server.JimServer;
import org.jim.server.command.CommandManager;
import org.jim.server.command.handler.ChatReqHandler;
import org.jim.server.command.handler.HandshakeReqHandler;
import org.jim.server.command.handler.LoginReqHandler;
import org.jim.server.config.ImServerConfig;
import org.jim.server.config.PropertyImServerConfigBuilder;
import org.jim.server.listener.ImServerListener;
import org.jim.server.processor.chat.DefaultAsyncChatMessageProcessor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(value =  1)
public class JIMStart implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {

//        ImServerConfig config = new PropertyImServerConfigBuilder("jim.properties")
//                .serverListener(new ServerListener()).build();
//        config.setImGroupListener(new GroupListener());
//        config.setImUserListener(new ClientListener());
//
//        JimServer jimServer = new JimServer(config);
//
//        registerHandler();
//
//        jimServer.start();

    }

    private void registerHandler() throws Exception {

        //添加自定义握手处理器;
        HandshakeReqHandler handshakeReqHandler = CommandManager.getCommand(Command.COMMAND_HANDSHAKE_REQ, HandshakeReqHandler.class);
        handshakeReqHandler.addMultiProtocolProcessor(new JIMWsHandshakeProcessor());

        //添加登录业务处理器;
        LoginReqHandler loginReqHandler = CommandManager.getCommand(Command.COMMAND_LOGIN_REQ, LoginReqHandler.class);
        loginReqHandler.setSingleProcessor(new LoginServiceProcessor());

        //添加用户业务聊天记录处理器，用户自己继承抽象类BaseAsyncChatMessageProcessor即可，以下为内置默认的处理器！
        ChatReqHandler chatReqHandler = CommandManager.getCommand(Command.COMMAND_CHAT_REQ, ChatReqHandler.class);
        chatReqHandler.setSingleProcessor(new DefaultAsyncChatMessageProcessor());



    }
}
